package com.jaya.budan.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jaya.budan.R;
import com.jaya.budan.adapter.LV_DongtaiTG_Adapter;
import com.jaya.budan.adapter.TaRen_HorizontalListViewAdapter;
import com.jaya.budan.base.BaseActivity;
import com.jaya.budan.base.MyApplication;
import com.jaya.budan.bean.ListUser_Information;
import com.jaya.budan.bean.User_Limit;
import com.jaya.budan.connect.RequestConnect;
import com.jaya.budan.utils.ToolUtils;
import com.jaya.budan.utils.Utils;
import com.jaya.budan.utils.VolleyUtil;
import com.jaya.budan.views.Circle_ImageView;
import com.jaya.budan.views.CommonPopWindow;
import com.jaya.budan.views.CustomListView;
import com.jaya.budan.views.ExpandableTextView;
import com.jaya.budan.views.HorizontalListView;
import com.tencent.imsdk.v2.V2TIMCallback;
import com.tencent.imsdk.v2.V2TIMConversation;
import com.tencent.imsdk.v2.V2TIMManager;
import com.tencent.imsdk.v2.V2TIMUserFullInfo;
import com.tencent.qcloud.tim.uikit.config.TUIKitConfigs;
import com.tencent.qcloud.tim.uikit.modules.chat.base.ChatInfo;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;

import cn.jiguang.share.android.api.JShareInterface;
import cn.jiguang.share.android.api.PlatActionListener;
import cn.jiguang.share.android.api.Platform;
import cn.jiguang.share.android.api.ShareParams;
import cn.jiguang.share.android.utils.Logger;
import cn.jiguang.share.qqmodel.QQ;
import cn.jiguang.share.wechat.Wechat;
import cn.jiguang.share.wechat.WechatMoments;
import cn.jiguang.share.weibo.SinaWeiboMessage;

public class TaRen_Main_Activity extends BaseActivity implements View.OnClickListener, CommonPopWindow.ViewClickListener {

    private RelativeLayout back, rl_geren_main, rl_geren_main_fans;
    private LinearLayout iv_geren_main_icon, ll_geren_main_ziliao, ll_gerentg_guanzhu, ll_gerentg_liaotian, ll_gerentg_taren,
            ll_geren_main_my_photo, ll_taren_main_no_black, ll_geren_main_tr_photo;
    private String access_token, type;
    private int uid;
    private Circle_ImageView iv_geren_main_header;
    private ExpandableTextView tv_geren_main_gxqm;
    private HorizontalListView main_hlv;
    private ImageView iv_geren_main_cover, iv_geren_main_is_vip, iv_geren_share;
    private TextView tv_geren_main_name, tv_geren_main_fans_count, tv_geren_main_age, tv_geren_main_distance, tv_geren_main_xingzuo, tv_taren_main_lock, tv_taren_main_black,
            tv_geren_main_mude, tv_geren_main_gqzk, tv_geren_main_guxiang, tv_geren_main_work, tv_geren_main_sflb, tv_geren_main_style, tv_geren_main_tixing, tv_geren_main_style2;
    private Button btn_gerentg_tgjl, btn_gerentg_ddjl;
    private CustomListView lv_geren_main_dongtai;
    private User_Limit user_limit;
    private List<ListUser_Information> user_informations;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            String toastMsg = (String) msg.obj;
            ToolUtils.showToast(TaRen_Main_Activity.this, toastMsg);
        }
    };
    public static Activity activity;
    private PlatActionListener mPlatActionListener = new PlatActionListener() {
        @Override
        public void onComplete(Platform platform, int action, HashMap<String, Object> data) {
            if(handler != null) {
                Message message = handler.obtainMessage();
                message.obj = "分享成功";
                handler.sendMessage(message);
            }
        }

        @Override
        public void onError(Platform platform, int action, int errorCode, Throwable error) {
            if(handler != null) {
                Message message = handler.obtainMessage();
                message.obj = "分享失败:" + (error != null ? error.getMessage() : "") + "---" + errorCode;
                Logger.dd("geren",message.obj+"");
                handler.sendMessage(message);
            }
        }

        @Override
        public void onCancel(Platform platform, int action) {
            if(handler != null) {
                Message message = handler.obtainMessage();
                message.obj = "分享取消";
                handler.sendMessage(message);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity = this;
        setContentView(R.layout.activity_taren_main);
        Intent intent = getIntent();
        access_token = intent.getStringExtra("access_token");
        uid = intent.getIntExtra("uid", 0);
        back = findViewById(R.id.back);
        rl_geren_main = findViewById(R.id.rl_geren_main);
        rl_geren_main_fans = findViewById(R.id.rl_geren_main_fans);
        iv_geren_main_cover = findViewById(R.id.iv_geren_main_cover);
        iv_geren_main_header = findViewById(R.id.iv_geren_main_header);
        iv_geren_main_icon = findViewById(R.id.iv_geren_main_icon);
        ll_geren_main_ziliao = findViewById(R.id.ll_geren_main_ziliao);
        ll_gerentg_guanzhu = findViewById(R.id.ll_gerentg_guanzhu);
        ll_gerentg_liaotian = findViewById(R.id.ll_gerentg_liaotian);
        ll_gerentg_taren = findViewById(R.id.ll_gerentg_taren);
        ll_taren_main_no_black = findViewById(R.id.ll_taren_main_no_black);
        ll_geren_main_my_photo = findViewById(R.id.ll_geren_main_my_photo);
        ll_geren_main_tr_photo = findViewById(R.id.ll_geren_main_tr_photo);
        btn_gerentg_tgjl = findViewById(R.id.btn_gerentg_tgjl);
        btn_gerentg_ddjl = findViewById(R.id.btn_gerentg_ddjl);
        lv_geren_main_dongtai = findViewById(R.id.lv_geren_main_dongtai);
        tv_geren_main_name = findViewById(R.id.tv_geren_main_name);
        tv_geren_main_fans_count = findViewById(R.id.tv_geren_main_fans_count);
        tv_geren_main_age = findViewById(R.id.tv_geren_main_age);
        tv_geren_main_distance = findViewById(R.id.tv_geren_main_distance);
        tv_geren_main_gxqm = findViewById(R.id.tv_geren_main_gxqm);
        tv_geren_main_xingzuo = findViewById(R.id.tv_geren_main_xingzuo);
        tv_taren_main_lock = findViewById(R.id.tv_taren_main_lock);
        iv_geren_main_is_vip = findViewById(R.id.iv_geren_main_is_vip);
        iv_geren_share = findViewById(R.id.iv_geren_share);
        tv_geren_main_mude = findViewById(R.id.tv_geren_main_mude);
        tv_geren_main_gqzk = findViewById(R.id.tv_geren_main_gqzk);
        tv_geren_main_guxiang = findViewById(R.id.tv_geren_main_guxiang);
        tv_geren_main_work = findViewById(R.id.tv_geren_main_work);
        tv_geren_main_sflb = findViewById(R.id.tv_geren_main_sflb);
        tv_geren_main_style = findViewById(R.id.tv_geren_main_style);
        tv_geren_main_tixing = findViewById(R.id.tv_geren_main_tixing);
        tv_geren_main_style2 = findViewById(R.id.tv_geren_main_style2);
        tv_taren_main_black = findViewById(R.id.tv_taren_main_black);
        main_hlv = findViewById(R.id.main_hlv);
        back.setOnClickListener(this);
        rl_geren_main_fans.setOnClickListener(this);
        btn_gerentg_tgjl.setOnClickListener(this);
        btn_gerentg_ddjl.setOnClickListener(this);
        iv_geren_main_cover.setOnClickListener(this);
        iv_geren_share.setOnClickListener(this);
        tv_taren_main_lock.setOnClickListener(this);
        ll_gerentg_guanzhu.setOnClickListener(this);
        ll_gerentg_liaotian.setOnClickListener(this);
        ll_geren_main_my_photo.setOnClickListener(this);
        rl_geren_main.bringChildToFront(iv_geren_main_icon);//显示在上层，不被遮挡
        rl_geren_main.updateViewLayout(iv_geren_main_icon, iv_geren_main_icon.getLayoutParams());
        type = "1";
        postData();
    }

    @SuppressLint({"RestrictedApi", "NonConstantResourceId"})
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_taren_main_lock:
                CommonPopWindow.newBuilder()
                        .setView(R.layout.pop_geren_fans_cancle)
                        .setAnimationStyle(R.style.AnimUp)
                        .setBackgroundDrawable(new BitmapDrawable())
                        .setSize(0, 600)
                        .setViewOnClickListener(this)
                        .setBackgroundDarkEnable(true)
                        .setBackgroundAlpha(0.7f)
                        .build(this)
                        .showAsCenter(v);
                break;
            case R.id.ll_geren_main_my_photo:
                CommonPopWindow.newBuilder()
                        .setView(R.layout.pop_geren_fans_cancle)
                        .setAnimationStyle(R.style.AnimUp)
                        .setBackgroundDrawable(new BitmapDrawable())
                        .setSize(0, 601)
                        .setViewOnClickListener(this)
                        .setBackgroundDarkEnable(true)
                        .setBackgroundAlpha(0.7f)
                        .build(this)
                        .showAsCenter(v);
                break;
            case R.id.rl_geren_main_fans:
                Intent intent1 = new Intent(this, GeRen_Main_Fans_Activity.class);
                intent1.putExtra("access_token", access_token);
                if (user_limit != null){
                    intent1.putExtra("uid", user_limit.getUid());
                    intent1.putExtra("fans", user_limit.getFans());
                    intent1.putExtra("watch", user_limit.getWatch());
                }
                intent1.putExtra("type", 1);
                intent1.putExtra("care", 2);
                startActivity(intent1);
                break;
            case R.id.btn_gerentg_tgjl:
                ll_geren_main_ziliao.setVisibility(View.GONE);
                lv_geren_main_dongtai.setVisibility(View.VISIBLE);
                ll_gerentg_taren.setVisibility(View.VISIBLE);
                btn_gerentg_tgjl.setBackgroundResource(R.drawable.login_login);
                btn_gerentg_ddjl.setBackgroundResource(R.drawable.login_exit);
                break;
            case R.id.btn_gerentg_ddjl:
                ll_geren_main_ziliao.setVisibility(View.VISIBLE);
                lv_geren_main_dongtai.setVisibility(View.GONE);
                ll_gerentg_taren.setVisibility(View.GONE);
                btn_gerentg_tgjl.setBackgroundResource(R.drawable.login_exit);
                btn_gerentg_ddjl.setBackgroundResource(R.drawable.login_login);
                break;
            case R.id.iv_geren_main_cover:
                Intent intent2 = new Intent(this, GeRen_Update_Msg_Cover_Activity.class);
                intent2.putExtra("access_token", access_token);
                startActivity(intent2);
                break;
            case R.id.ll_gerentg_guanzhu:
                JSONObject object = new JSONObject();
                try {
                    object.put("uid", user_limit.getUid());
                    object.put("type", type);//1 关注 2取消关注
                    object.put("flag", 1);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                postUserFansData(object);
                break;
            case R.id.ll_gerentg_liaotian:
                V2TIMUserFullInfo v2TIMUserFullInfo = new V2TIMUserFullInfo();
                v2TIMUserFullInfo.setFaceUrl(user_limit.getHead());
                v2TIMUserFullInfo.setNickname("" + user_limit.getUsername());
                v2TIMUserFullInfo.setSelfSignature(user_limit.getRemark() + "");
                V2TIMManager.getInstance().setSelfInfo(v2TIMUserFullInfo, new V2TIMCallback() {
                    @Override
                    public void onError(int code, String desc) {
                        ToolUtils.showToast(TaRen_Main_Activity.this, "Error code = " + code + ", desc = " + desc);
                    }
                    @Override
                    public void onSuccess() {
                        TUIKitConfigs.getConfigs().getGeneralConfig().setUserFaceUrl(user_limit.getHead());
                        TUIKitConfigs.getConfigs().getGeneralConfig().setUserNickname(user_limit.getNicename());
                        TUIKitConfigs.getConfigs().getGeneralConfig().setUserSig(user_limit.getRemark());
                        TUIKitConfigs.getConfigs().getGeneralConfig().setUserId(user_limit.getUid() + "");
                        SharedPreferences preferences = getSharedPreferences("user_message", Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor = preferences.edit();
//                        editor.putString(ToolUtils.getTencentUserDBKey(tencentUser.getUserId()), JSON.toJSONString(tencentUser));
                        editor.putString("user_limit_" + user_limit.getUid(), JSON.toJSONString(user_limit));
                        Log.d("user_limit json = ",JSON.toJSONString(user_limit));
                        editor.apply();
                    }
                });
                //此处为demo的实现逻辑，更根据会话类型跳转到相关界面，开发者可根据自己的应用场景灵活实现
                ChatInfo chatInfo = new ChatInfo();
//                chatInfo.setType(conversationInfo.isGroup() ? V2TIMConversation.V2TIM_GROUP : V2TIMConversation.V2TIM_C2C);
                chatInfo.setType(V2TIMConversation.V2TIM_C2C);
                chatInfo.setId(user_limit.getUid() + "");
                chatInfo.setChatName(user_limit.getNicename());
                Intent intent = new Intent(this, Chat_Activity.class);
                intent.putExtra("access_token", access_token);
                intent.putExtra("roomId", user_limit.getUid() + "");
                intent.putExtra("chatInfo", chatInfo);
                intent.putExtra("user_limit", user_limit);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                break;
            case R.id.iv_geren_share:
                CommonPopWindow.newBuilder()
                        .setView(R.layout.pop_geren_main_taren_share)
                        .setAnimationStyle(R.style.AnimUp)
                        .setBackgroundDrawable(new BitmapDrawable())
                        .setSize(0, 800)
                        .setViewOnClickListener(this)
                        .setBackgroundDarkEnable(true)
                        .setBackgroundAlpha(0.7f)
                        .build(this)
                        .showAsBottom(v);
                break;
            case R.id.back:
                finish();
                break;
            default:
                break;
        }
    }

    /**
     * 网络请求
     */
    private void postData() {
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, RequestConnect.gr_user_information_xq_Url + access_token + "&uid=" + uid, null, new Response.Listener<JSONObject>() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.d("geren_main", jsonObject.toString());
                try {
                    int code = jsonObject.optInt("code");
                    int status = jsonObject.optInt("status");
                    String message = jsonObject.getString("message");
                    if (code == 0) {
                        Gson gson = new Gson();
                        JSONObject info = jsonObject.getJSONObject("info");
                        user_limit = gson.fromJson(info.toString(), User_Limit.class);
                        Glide.with(TaRen_Main_Activity.this).load(user_limit.getHead()).apply(new RequestOptions()
                                .placeholder(R.mipmap.fill_person_icon).error(R.mipmap.fill_person_icon)
                                .diskCacheStrategy(DiskCacheStrategy.NONE)//关闭Glide的硬盘缓存机制
                        ).into(iv_geren_main_header);
                        Glide.with(TaRen_Main_Activity.this).load(user_limit.getBackground()).apply(new RequestOptions()
                                .placeholder(R.mipmap.banner).error(R.mipmap.banner)
                                .diskCacheStrategy(DiskCacheStrategy.NONE)//关闭Glide的硬盘缓存机制
                        ).into(iv_geren_main_cover);
                        tv_geren_main_name.setText("" + user_limit.getNicename());
                        if (user_limit.getIs_vip() == 1){
                            iv_geren_main_is_vip.setVisibility(View.VISIBLE);
                        } else {
                            iv_geren_main_is_vip.setVisibility(View.GONE);
                        }
                        if (user_limit.getIs_black() == 1){ //1被拉黑
                            tv_geren_main_age.setVisibility(View.GONE);
                            tv_geren_main_distance.setVisibility(View.GONE);
                            ll_taren_main_no_black.setVisibility(View.GONE);
                            tv_taren_main_black.setVisibility(View.VISIBLE);
                        } else {
                            tv_geren_main_age.setVisibility(View.VISIBLE);
                            tv_geren_main_distance.setVisibility(View.VISIBLE);
                            ll_taren_main_no_black.setVisibility(View.VISIBLE);
                            tv_taren_main_black.setVisibility(View.GONE);
                        }
                        tv_geren_main_fans_count.setText("" + user_limit.getFans());
                        tv_geren_main_age.setText("" + user_limit.getAge() + "/" + user_limit.getHeight() + "/" + user_limit.getWeigh() + "/" + user_limit.getType() + "/" + user_limit.getRole());
                        if (user_limit.getIs_online() == 0){
                            tv_geren_main_distance.setText("" + user_limit.getDistance() + "/离线");
                        } else {
                            tv_geren_main_distance.setText("" + user_limit.getDistance() + "/在线");
                        }
                        tv_geren_main_gxqm.setText("个性签名：" + user_limit.getRemark());
                        tv_geren_main_xingzuo.setText("" + user_limit.getConstellatory());
                        tv_geren_main_mude.setText("" + user_limit.getAim());
                        tv_geren_main_gqzk.setText("" + user_limit.getStatus());
                        tv_geren_main_guxiang.setText("" + user_limit.getProvince() + user_limit.getCity() + user_limit.getDistrict());
                        tv_geren_main_work.setText("" + user_limit.getWork());
                        tv_geren_main_sflb.setText("" + user_limit.getIdentity());
                        tv_geren_main_style.setText("" + user_limit.getStyle());
                        tv_geren_main_tixing.setText("" + user_limit.getI_face());
                        tv_geren_main_style2.setText("" + user_limit.getI_style());
                        postTGData(user_limit.getUid());
                        JSONObject object = new JSONObject();
                        try {
                            object.put("uid", user_limit.getUid());
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        postPhotoData(object);
                    } else {
                        ToolUtils.showToast(TaRen_Main_Activity.this, message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                ToolUtils.showToast(TaRen_Main_Activity.this, "网络请求失败，请稍后重试~");
            }
        });
        request.setTag(this);
        VolleyUtil.getQueue(this).add(request);
    }

    /**
     * 网络请求
     */
    private void postTGData(int id) {
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, RequestConnect.gr_my_dongtai_Url + access_token + "&page=1&limit=10&uid=" + id, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.d("dongtai", jsonObject.toString());
                try {
                    int code = jsonObject.optInt("code");
                    int status = jsonObject.optInt("status");
                    String message = jsonObject.getString("message");
                    if (code == 0) {
                        Gson gson = new Gson();
                        JSONArray info = jsonObject.getJSONArray("info");
                        user_informations = gson.fromJson(info.toString(), new TypeToken<List<ListUser_Information>>(){}.getType());
                        lv_geren_main_dongtai.setAdapter(new LV_DongtaiTG_Adapter(TaRen_Main_Activity.this, user_informations, 1, access_token));
                    } else {
                        ToolUtils.showToast(TaRen_Main_Activity.this, message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                ToolUtils.showToast(TaRen_Main_Activity.this, "网络请求失败，请稍后重试~");
            }
        });
        request.setTag(this);
        VolleyUtil.getQueue(this).add(request);
    }

    /**
     * 网络请求
     */
    private void postPhotoData(JSONObject object) {
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, RequestConnect.tr_user_information_photo_Url + access_token, object, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.d("dongtai", jsonObject.toString());
                try {
                    int code = jsonObject.optInt("code");
                    int status = jsonObject.optInt("status");
                    String message = jsonObject.getString("message");
                    if (code == 0) {
                        Gson gson = new Gson();
                        JSONObject info = jsonObject.getJSONObject("info");
                        int is_support = info.getInt("is_support");
                        int photoStatus = info.getInt("photoStatus");
                        int photoHots = info.getInt("photoHots");
                        JSONArray files = info.getJSONArray("files");
                        List<ListUser_Information> user_informations = gson.fromJson(files.toString(), new TypeToken<List<ListUser_Information>>(){}.getType());
                        if (user_informations.size() > 0){
                            ll_geren_main_tr_photo.setVisibility(View.VISIBLE);
                            ll_geren_main_my_photo.setVisibility(View.GONE);
                        } else {
                            ll_geren_main_tr_photo.setVisibility(View.GONE);
                            ll_geren_main_my_photo.setVisibility(View.VISIBLE);
                        }
                        // 获取屏幕宽高
                        int screenWidth = getWindowManager().getDefaultDisplay().getWidth(); // 屏幕宽（像素，如：480px）
                        TaRen_HorizontalListViewAdapter horizontalListViewAdapter = new TaRen_HorizontalListViewAdapter(TaRen_Main_Activity.this, screenWidth, user_informations, is_support);
                        main_hlv.setAdapter(horizontalListViewAdapter);
                        main_hlv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                            }
                        });
                    } else {
                        ToolUtils.showToast(TaRen_Main_Activity.this, message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                ToolUtils.showToast(TaRen_Main_Activity.this, "网络请求失败，请稍后重试~");
            }
        });
        request.setTag(this);
        VolleyUtil.getQueue(this).add(request);
    }

    /**
     * 网络请求
     */
    private void postSQPhotoLockData(JSONObject object) {
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, RequestConnect.tr_user_information_Url + access_token, object, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.d("dongtai", jsonObject.toString());
                try {
                    int code = jsonObject.optInt("code");
                    int status = jsonObject.optInt("status");
                    String message = jsonObject.getString("message");
                    if (code == 0) {
                        Gson gson = new Gson();
                        JSONArray info = jsonObject.getJSONArray("info");
                        ToolUtils.showToast(TaRen_Main_Activity.this, message);
                        JSONObject object = new JSONObject();
                        try {
                            object.put("uid", user_limit.getUid());
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        postPhotoData(object);
                    } else {
                        ToolUtils.showToast(TaRen_Main_Activity.this, message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                ToolUtils.showToast(TaRen_Main_Activity.this, "网络请求失败，请稍后重试~");
            }
        });
        request.setTag(this);
        VolleyUtil.getQueue(this).add(request);
    }

    /**
     * 网络请求
     */
    private void postUserFansData(JSONObject object) {
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, RequestConnect.lt_edit_fans_Url + access_token, object, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.d("dongtai_fans", jsonObject.toString());
                try {
                    int code = jsonObject.optInt("code");
                    int status = jsonObject.optInt("status");
                    String message = jsonObject.getString("message");
                    if (code == 0) {
                        Gson gson = new Gson();
                        JSONArray info = jsonObject.getJSONArray("info");
                        ToolUtils.showToast(TaRen_Main_Activity.this, message);
                        type = "2";
                    } else {
                        ToolUtils.showToast(TaRen_Main_Activity.this, message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                ToolUtils.showToast(TaRen_Main_Activity.this, "网络请求失败，请稍后重试~");
            }
        });
        request.setTag(this);
        VolleyUtil.getQueue(this).add(request);
    }

    /**
     * 网络请求
     */
    private void postSetFansBZData(JSONObject object) {
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, RequestConnect.lt_set_fans_bz_Url + access_token, object, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.d("dongtai_set", jsonObject.toString());
                try {
                    int code = jsonObject.optInt("code");
                    int status = jsonObject.optInt("status");
                    String message = jsonObject.getString("message");
                    if (code == 0) {
                        Gson gson = new Gson();
                        JSONArray info = jsonObject.getJSONArray("info");
                        ToolUtils.showToast(TaRen_Main_Activity.this, message);
                    } else {
                        ToolUtils.showToast(TaRen_Main_Activity.this, message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                ToolUtils.showToast(TaRen_Main_Activity.this, "网络请求失败，请稍后重试~");
            }
        });
        request.setTag(this);
        VolleyUtil.getQueue(this).add(request);
    }

    /**
     * 网络请求
     */
    private void postSetBlackUserData(JSONObject object) {
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, RequestConnect.tr_user_information_black_user_Url + access_token, object, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.d("dongtai_black", jsonObject.toString());
                try {
                    int code = jsonObject.optInt("code");
                    int status = jsonObject.optInt("status");
                    String message = jsonObject.getString("message");
                    if (code == 0) {
                        Gson gson = new Gson();
                        JSONArray info = jsonObject.getJSONArray("info");
                        ToolUtils.showToast(TaRen_Main_Activity.this, message);//被拉黑
                    } else {
                        ToolUtils.showToast(TaRen_Main_Activity.this, message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                ToolUtils.showToast(TaRen_Main_Activity.this, "网络请求失败，请稍后重试~");
            }
        });
        request.setTag(this);
        VolleyUtil.getQueue(this).add(request);
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void getChildView(final PopupWindow mPopupWindow, View view, int mLayoutResId) {
        switch (mLayoutResId) {
            case R.layout.pop_geren_fans_cancle:
                TextView tv_cancle = view.findViewById(R.id.tv_cancle);
                TextView tv_geren_fans_cancle = view.findViewById(R.id.tv_geren_fans_cancle);
                TextView img_guanbi = view.findViewById(R.id.img_guanbi);
                tv_geren_fans_cancle.setText("用户设置了访问权限");
                tv_cancle.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPopupWindow.dismiss();
                    }
                });
                img_guanbi.setText("申请解锁");
                img_guanbi.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPopupWindow.dismiss();
                        JSONObject object = new JSONObject();
                        try {
                            object.put("uid", user_limit.getUid());
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        postSQPhotoLockData(object);
                    }
                });
                break;
            case R.layout.pop_geren_main_taren_share:
                List<String> platformList = JShareInterface.getPlatformList();
                if (platformList == null || platformList.isEmpty()) {
                    ToolUtils.showToast(this, "请检查配置文件是否正确");
                    finish();
                    return;
                }
                ShareParams shareParams = new ShareParams();
                shareParams.setUrl("https://www.jiguang.cn");
                shareParams.setTargetPkg("com.tencent.mm");
                shareParams.setTargetClass("com.tencent.mm.ui.LauncherUI");
                shareParams.setExtra("this message from jshare.");
                LinearLayout ll_pop_cancel = view.findViewById(R.id.ll_pop_cancel);
                LinearLayout ll_pop_geren_haoyou = view.findViewById(R.id.ll_pop_geren_haoyou);
                LinearLayout ll_pop_geren_wx = view.findViewById(R.id.ll_pop_geren_wx);
                LinearLayout ll_pop_geren_pyq = view.findViewById(R.id.ll_pop_geren_pyq);
                LinearLayout ll_pop_geren_qq = view.findViewById(R.id.ll_pop_geren_qq);
                LinearLayout ll_pop_geren_wb = view.findViewById(R.id.ll_pop_geren_wb);
                LinearLayout ll_pop_geren_szbz = view.findViewById(R.id.ll_pop_geren_szbz);
                LinearLayout ll_pop_geren_qxgz = view.findViewById(R.id.ll_pop_geren_qxgz);
                LinearLayout ll_pop_geren_hmd = view.findViewById(R.id.ll_pop_geren_hmd);
                LinearLayout ll_pop_geren_qxsz = view.findViewById(R.id.ll_pop_geren_qxsz);
                LinearLayout ll_pop_geren_jb = view.findViewById(R.id.ll_pop_geren_jb);
                ll_pop_geren_wx.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //只有微信支持表情
                        shareParams.setShareType(Platform.SHARE_EMOJI);
                        shareParams.setImagePath(MyApplication.ImagePath);
                        shareParams.setFilePath(MyApplication.ImagePath);
                        //只有微信支持文件
                        shareParams.setShareType(Platform.SHARE_FILE);
                        shareParams.setFilePath(MyApplication.ImagePath);
                        //只有微信支持小程序分享
//                        shareParams.setShareType(Platform.SHARE_MINI_PROGRAM);
//                        shareParams.setTitle("极光分享");
//                        shareParams.setText("分享");
//                        shareParams.setUrl("https://www.jiguang.cn");
//                        shareParams.setImagePath(MyApplication.ImagePath);
//                        shareParams.setMiniProgramWithShareTicket(false);//可以设置withShareTicket为true，当分享卡片在群聊中被其他用户打开时，可以获取到shareTicket
//                        shareParams.setMiniProgramType(0);// 正式版:0，开发版（测试版）:1，体验版（预览版）:2
//                        shareParams.setMiniProgramImagePath(MyApplication.ImagePath);//小程序图片地址
//                        shareParams.setMiniProgramPath("pages/index/index");//小程序页面路径
//                        shareParams.setMiniProgramUserName("gh_cd370c00d3d4");//小程序原始id·
                        JShareInterface.share(Wechat.Name, shareParams, mPlatActionListener);
                    }
                });
                ll_pop_geren_pyq.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //只有微信支持文件
                        shareParams.setShareType(Platform.SHARE_FILE);
                        JShareInterface.share(WechatMoments.Name, shareParams, mPlatActionListener);
                    }
                });
                ll_pop_geren_qq.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //只有支持QQ APP
                        shareParams.setShareType(Platform.SHARE_APPS);
                        shareParams.setImagePath(MyApplication.ImagePath);
                        JShareInterface.share(QQ.Name, shareParams, mPlatActionListener);
//                        JShareInterface.share(QZone.Name, shareParams, mPlatActionListener);//QQ空间
                    }
                });
                ll_pop_geren_wb.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        shareParams.setTitle("极光分享");
                        shareParams.setText("分享");
                        shareParams.setUrl("https://www.jiguang.cn");
                        shareParams.setShareType(Platform.SHARE_WEBPAGE);
                        shareParams.setImagePath(MyApplication.ImagePath);
                        JShareInterface.share(SinaWeiboMessage.Name, shareParams, mPlatActionListener);
                    }
                });
                ll_pop_cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPopupWindow.dismiss();
                    }
                });
                ll_pop_geren_hmd.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        JSONObject object = new JSONObject();
                        try {
                            object.put("uid", user_limit.getUid());
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        postSetBlackUserData(object);
                    }
                });
                ll_pop_geren_szbz.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPopupWindow.dismiss();
                        CommonPopWindow.newBuilder()
                                .setView(R.layout.pop_geren_update_msg_name)
                                .setAnimationStyle(R.style.AnimUp)
                                .setBackgroundDrawable(new BitmapDrawable())
                                .setSize(0, 600)
                                .setViewOnClickListener(TaRen_Main_Activity.this)
                                .setBackgroundDarkEnable(true)
                                .setBackgroundAlpha(0.7f)
                                .build(TaRen_Main_Activity.this)
                                .showAsCenter(view);
                    }
                });
                ll_pop_geren_qxgz.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        JSONObject object = new JSONObject();
                        try {
                            object.put("uid", user_limit.getUid());
                            object.put("type", 1);//1 关注 2取消关注
                            object.put("flag", 1);//悄悄关注标识
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        postUserFansData(object);
                    }
                });
                ll_pop_geren_qxsz.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPopupWindow.dismiss();
                        Intent intent = new Intent(TaRen_Main_Activity.this, TaRen_Set_Permission_Activity.class);
                        intent.putExtra("access_token", access_token);
                        startActivity(intent);
                    }
                });
                ll_pop_geren_jb.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(TaRen_Main_Activity.this, TaRen_Set_Report_Activity.class);
                        intent.putExtra("access_token", access_token);
                        intent.putExtra("uid", user_limit.getUid());
                        startActivity(intent);
                    }
                });
//                shareParams.setShareType(Platform.SHARE_IMAGE);
//                shareParams.setImageUrl("");
//                JShareInterface.share("", shareParams, mPlatActionListener);
                break;
            case R.layout.pop_geren_update_msg_name:
                EditText tv_geren_fans_cancle1 = view.findViewById(R.id.tv_geren_fans_cancle);
                TextView tv_cancle1 = view.findViewById(R.id.tv_cancle);
                TextView tv_pop_update_msg_name_title = view.findViewById(R.id.tv_pop_update_msg_name_title);
                TextView img_guanbi1 = view.findViewById(R.id.img_guanbi);
                TextView tv_pop_geren_update_name_count = view.findViewById(R.id.tv_pop_geren_update_name_count);
                tv_pop_update_msg_name_title.setText("备注");
                tv_geren_fans_cancle1.setHint("请输入备注");
                tv_cancle1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPopupWindow.dismiss();
                    }
                });
                img_guanbi1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPopupWindow.dismiss();
                        String s = tv_geren_fans_cancle1.getText().toString().trim();
                        if (s.equals("")){
                            ToolUtils.showToast(TaRen_Main_Activity.this, "备注名不能为空");
                            return;
                        } else {
                            ToolUtils.showToast(TaRen_Main_Activity.this, "" + s);
                            JSONObject object = new JSONObject();
                            try {
                                object.put("t_uid", user_limit.getUid());
                                object.put("name", s);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            postSetFansBZData(object);
                        }
                    }
                });
                tv_geren_fans_cancle1.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                    }

                    @SuppressLint("SetTextI18n")
                    @Override
                    public void afterTextChanged(Editable s) {
                        tv_pop_geren_update_name_count.setText(s.length() + "/20");
                    }
                });
                break;
            default:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(handler != null){
            handler.removeCallbacksAndMessages(null);
            handler = null;
        }
    }
}
